package com.telecom.cms.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;

import com.telecom.cms.dto.SysLocLatn;

import com.telecom.cms.util.Constant;

public class SysLocLatnDAO {
	private static List latnlist;

	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(SysLocLatn transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Sys_Loc_Latn where latn_id="
				+ transientInstance.getLatnid();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Sys_Loc_Latn set latn_id='"

				+ transientInstance.getLatnid() + "',latn_name='"
						+ transientInstance.getLatnname() + "',area_code='"
						+ transientInstance.getArea_Code() + "',latn_cd='"
						+ transientInstance.getLatn_Cd() + "',latn_desc="
						+ transientInstance.getLatn_Desc() + " where latn_id="
						+ transientInstance.getLatnid();
			else
				sql = "insert into " + db + ".Sys_Loc_Latn values("
						+ transientInstance.getLatnid() + ","
						+ transientInstance.getLatnname() + ","
						+ transientInstance.getArea_Code() + ","
						+ transientInstance.getLatn_Cd() + ","
						+ transientInstance.getLatn_Desc() + ")";
			rs = dao.insert(sql);
			
			if (rs2 != null) {

				rs2.close();
			}
			

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}finally
		{
			dao.closeconn();
		}
		return rs;

	}

	public void delete(SysLocLatn persistentInstance) {

		String sql = "delete from " + db + ".Sys_Loc_Latn where Code_Id = "
				+ persistentInstance.getLatnid();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public SysLocLatn findById(String id) {
		if(latnlist==null)
			findAll();
		for(int i=0;i<latnlist.size();i++)
		{
			if(((SysLocLatn)latnlist.get(i)).getLatnid().equals(id))
				return (SysLocLatn)latnlist.get(i);
		}
		return null;


		/*String sql = "select * from " + db + ".Sys_Loc_Latn where Latn_Id = "
				+ id;
		SysLocLatn sd = new SysLocLatn();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				sd.setLatnid(id);
				sd.setLatnname(rs.getString(2));
				sd.setArea_Code(rs.getString(3));
				sd.setLatn_Cd(rs.getString(4));
				sd.setLatn_Desc(rs.getString(5));

			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return sd;*/

	}

	public List findAll() {
        if(latnlist!=null)
        	return latnlist;
		String sql = "select * from " + db + ".Sys_Loc_Latn";

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				SysLocLatn sd = new SysLocLatn();

				sd.setLatnid(rs.getString(1));
				sd.setLatnname(rs.getString(2));
				sd.setArea_Code(rs.getString(3));
				sd.setLatn_Cd(rs.getString(4));
				sd.setLatn_Desc(rs.getString(5));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		latnlist=list;
		return list;

	}
	
	public SysLocLatn findById(String idtype,String id) {

		if(latnlist==null)
			findAll();
		String idtyp=null;
		for(int i=0;i<latnlist.size();i++)
		{
			if(idtype.equals("latn_id"))
			   idtyp=((SysLocLatn)latnlist.get(i)).getLatnid();
			if(idtype.equals("latn_cd"))
				   idtyp=((SysLocLatn)latnlist.get(i)).getLatn_Cd();
			if(idtyp.equals(id))
				return (SysLocLatn)latnlist.get(i);
		}
		return null;
		/*
		String sql = "select * from " + db + ".Sys_Loc_Latn where "+idtype+" = '" + id + "'";
		SysLocLatn sd = new SysLocLatn();
		try {
			ResultSet rs = dao.select(sql);
			if (rs.next()) {
				

				sd.setLatnid(rs.getString(1));
				sd.setLatnname(rs.getString(2));
				sd.setArea_Code(rs.getString(3));
				sd.setLatn_Cd(rs.getString(4));
				sd.setLatn_Desc(rs.getString(5));
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return sd;*/

	}
	public List findAll(String latnId) {

		String sql = "select * from " + db + ".Sys_Loc_Latn where Latn_Id = '" + latnId + "'";

		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				SysLocLatn sd = new SysLocLatn();

				sd.setLatnid(rs.getString(1));
				sd.setLatnname(rs.getString(2));
				sd.setArea_Code(rs.getString(3));
				sd.setLatn_Cd(rs.getString(4));
				sd.setLatn_Desc(rs.getString(5));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return list;

	}
	
	public HashMap findAllMap() {
		HashMap result=new HashMap();
		String sql = "select * from " + db + ".Sys_Loc_Latn";

		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				SysLocLatn sd = new SysLocLatn();

				sd.setLatnid(rs.getString(1));
				sd.setLatnname(rs.getString(2));
				sd.setArea_Code(rs.getString(3));
				sd.setLatn_Cd(rs.getString(4));
				sd.setLatn_Desc(rs.getString(5));
				result.put(rs.getString(1), sd);
			}
			if (rs != null) {

				rs.close();
			}
			

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally
		{
			dao.closeconn();
		}
		return result;

	}

}